package com.bishe.demo.modules.other.dao;

import com.bishe.demo.modules.common.entity.SearchBean;
import com.bishe.demo.modules.other.entity.Message;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface MessageDao {
    @Insert("insert into other_message values(null,#{messageDetail},#{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "messageId", keyColumn = "message_id")
    void insertMessage(Message message);

    @Update("update other_message set message_detail=#{messageDetail} where message_id=#{messageId}")
    void updateMessage(Message message);

    @Delete("delete from other_message where message_id=#{messageId}")
    void deleteMessageByMessageId(int messageId);

    @Select("select * from other_message where message_id=#{messageId}")
    Message getMessageByMessageId(int messageId);

    @Select("select * from other_message om left join other_user_message oum on oum.message_id=om.message_id where oum.user_id=#{userId}")
    List<Message> getMessagesByUserId(int userId);

    @Select("select * from other_message om left join other_scenery_message osm on osm.message_id=om.message_id where osm.scenery_id=#{sceneryId}")
    List<Message> getMessagesBySceneryId(int sceneryId);

    @Select("<script>"
            + "select * from other_message "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (message_detail like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by message_id asc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Message> getMessagesBySearchBean(SearchBean searchBean);
}
